package com.musollo.mp3tools;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.apache.log4j.Logger;

import com.musollo.exceptions.CriteriaException;

public class UpdateCriteria implements Iterable<Integer> {
	public static final int LENGTH = 1;
	public static final int TITLE = 3;
	public static final int ARTIST = 4;
	public static final int ALBUM = 5;
	public static final int ALBUMTRACKNUMBER = 6;
	public static final int YEAR = 7;
	public static final int GENRE = 8;

	private final static Logger LOGGER = Logger.getLogger(UpdateCriteria.class);

	private final Set<Integer> updateCriteria;

	public UpdateCriteria() {
		this.updateCriteria = new HashSet<Integer>();
		reset();
	}

	@Override
	public Iterator<Integer> iterator() {
		return this.updateCriteria.iterator();
	}

	public void removeLowestCriteria() throws CriteriaException {
		if (this.updateCriteria.size() == 0) {
			throw new CriteriaException("There are no criteria left");
		}

		int lowestCriteria = UpdateCriteria.LENGTH;

		for (Integer criteriaPriority : this.updateCriteria) {
			if (criteriaPriority > lowestCriteria) {
				lowestCriteria = criteriaPriority;
			}
		}

		this.updateCriteria.remove(Integer.valueOf(lowestCriteria));

		LOGGER.trace("Removed lowest criteria: " + lowestCriteria);
	}

	public void reset() {
		LOGGER.debug("Resetting criteria");

		this.updateCriteria.clear();
		this.updateCriteria.add(UpdateCriteria.LENGTH);
		this.updateCriteria.add(UpdateCriteria.ALBUM);
		this.updateCriteria.add(UpdateCriteria.ARTIST);
		this.updateCriteria.add(UpdateCriteria.ALBUMTRACKNUMBER);
		this.updateCriteria.add(UpdateCriteria.GENRE);
		this.updateCriteria.add(UpdateCriteria.TITLE);
		this.updateCriteria.add(UpdateCriteria.YEAR);
	}
}
